package com.easylinkin.linkappapi.quality.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import com.easylinkin.linkappapi.quality.dto.ActualMeasureInfoDTO;
import com.easylinkin.linkappapi.quality.dto.ActualMeasureTopDTO;
import com.easylinkin.linkappapi.quality.entity.ActualMeasureInfo;
import com.easylinkin.linkappapi.quality.entity.vo.ActualMeasureInfoVo;
import com.easylinkin.linkappapi.quality.service.ActualMeasureInfoService;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * ActualMeasureInfo表控制层
 *
 * @author zhengwen
 * @date 2022/09/01
 */
@Slf4j
@RestController
@RequestMapping("/actualMeasureInfo")
public class ActualMeasureInfoController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ActualMeasureInfoService actualMeasureInfoService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<ActualMeasureInfo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ActualMeasureInfo> record = actualMeasureInfoService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.actualMeasureInfoService.getOneById(id))).build();
    }

    /**
     * 新增数据
     *
     * @param appQualityActualMeasureInfo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增")
    public RestMessage insert(@RequestBody ActualMeasureInfo appQualityActualMeasureInfo) {
        return RestBuilders.successBuilder().success((this.actualMeasureInfoService.saveOne(appQualityActualMeasureInfo))).build();
    }

    /**
     * 修改数据
     *
     * @param appQualityActualMeasureInfo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody ActualMeasureInfo appQualityActualMeasureInfo) {
        return RestBuilders.successBuilder().success(this.actualMeasureInfoService.updateOne(appQualityActualMeasureInfo)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    @CommonOperateLogAnnotate(module = LogConstant.LogModule.QUALITY_MANAGEMENT, desc = "")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        //记录日志
        if(CollectionUtil.isNotEmpty(idList)){
            List<String> actualNames = new ArrayList<>();
            idList.stream().forEach(d->{
                //RestMessage rs = this.actualMeasureInfoService.getOneDto(d);
                //ActualMeasureInfoDTO dto = JSONUtil.toBean(JSONUtil.toJsonStr(rs.getData().toString()),ActualMeasureInfoDTO.class);
                ActualMeasureInfoDTO dto = actualMeasureInfoService.getOneDtoById(d);
                if(dto != null && dto.getPosition() != null){
                    actualNames.add(dto.getPosition().getName());
                }
            });
            LogHelper.setContent(LogConstant.LogOperateType.SAFE_QUALITY_ACTUAL_INFO_DELETE, StringUtil.join(actualNames,","));
        }
        return RestBuilders.successBuilder().success(this.actualMeasureInfoService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ActualMeasureInfo appQualityActualMeasureInfo, HttpServletRequest request, HttpServletResponse response) {
        actualMeasureInfoService.export(appQualityActualMeasureInfo, request, response);
    }

    /**
     * 保存实测实量数据
     *
     * @param actualMeasureInfoVo 实测实量vo
     * @return 统一出参
     */
    @PostMapping("/saveMeasureInfoData")
    @ApiOperation("保存实测实量数据")
    public RestMessage saveMeasureInfoData(@RequestBody ActualMeasureInfoVo actualMeasureInfoVo) {
        log.info("--保存实测实量数据--");
        return this.actualMeasureInfoService.saveMeasureInfoData(actualMeasureInfoVo);
    }

    /**
     * 分页查询dto所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/selectDtoPage")
    @ApiOperation("查询dto分页")
    public RestMessage selectDtoPage(@RequestBody RequestModel<ActualMeasureInfoVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<ActualMeasureInfoDTO> record = actualMeasureInfoService.selectDtoPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 根据分包单位查询排行
     *
     * @return 根据分包单位查询排行
     */
    @GetMapping("/selectTopByOrg")
    public RestMessage selectTopByOrg() {
        List<ActualMeasureTopDTO> record = actualMeasureInfoService.selectTopByOrg();
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 根据分包类型查询排行
     *
     * @return 根据分包类型查询排行
     */
    @GetMapping("/selectTopByType")
    public RestMessage selectTopByType() {
        List<ActualMeasureTopDTO> record = actualMeasureInfoService.selectTopByType();
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 根据实测实量id查询单条信息dto
     * @param id 主键
     * @return 统一出参
     */
    @GetMapping("/getOneDto/{id}")
    @ApiOperation("查询单条")
    public RestMessage getOneDto(@PathVariable Long id) {
        return actualMeasureInfoService.getOneDto(id);
    }

    /**
     * 查询Tab数量
     *
     * @param actualMeasureInfoVo 查询条件vo
     * @return tab数据数量
     */
    @PostMapping("/selectTabNum")
    @ApiOperation("查询Tab数量")
    public RestMessage selectTabNum(@RequestBody ActualMeasureInfoVo actualMeasureInfoVo) {
        return actualMeasureInfoService.selectTabNum(actualMeasureInfoVo);
    }
}

